﻿using System.Collections.Generic;
using System.Drawing;
using System;
using System.Reflection;
using System.IO;

namespace TripleGame.GUI
{
    class ImageUtils
    {
        private static Dictionary<string, Image> images =
            new Dictionary<string, Image>();

        public static Image GetImage(char letter)
        {
            string imageFileName;
            switch (letter)
            {
                case 'A':
                    imageFileName = "A.png";
                    break;
                case 'a':
                    imageFileName = "A-inverted.png";
                    break;
                case 'B':
                    imageFileName = "B.png";
                    break;
                case 'b':
                    imageFileName = "B-inverted.png";
                    break;
                case ' ':
                    imageFileName = "Empty.png";
                    break;
                default:
                    throw new ArgumentException("Invalid letter: " + letter);
            }

            if (images.ContainsKey(imageFileName))
            {
                return images[imageFileName];
            }

            Image img = LoadImage(imageFileName);
            images[imageFileName] = img;
            return img;
        }

        private static Image LoadImage(string imageFileName)
        {
            Assembly currentAssembly = Assembly.GetExecutingAssembly();
            string imageResourceName = typeof(ImageUtils).Namespace + 
                ".Images." + imageFileName;
            Stream imageStream = currentAssembly.
                GetManifestResourceStream(imageResourceName);
            using (imageStream)
            {
                Image img = Image.FromStream(imageStream);
                return img;
            }
        }
    }
}
